package com.hcb.tonguetiphealth.adapter;

import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hcb.tonguetiphealth.manager.AsyncBitmapLoader;
import com.hcb.tonguetiphealth.manager.AsyncBitmapLoader.ImageCallBack;
import com.hcb.tonguetiphealth.model.Drinks;
import com.hcb.tonguetiphealth.view.R;
import com.hcb.tonguetiphealth.widget.drinksbook.DrinksBookCartWidget;

public class DrinksBookCartListAdapter extends BaseAdapter {

	private Context mContext;
	private List<Drinks> mList;
	private LayoutInflater vi;
	private DrinksBookCartWidget drinksBookCartWidget;
	private AsyncBitmapLoader asyncBitmapLoader;
	private DrinksBookCartListAdapter mThis;
	private Builder mDialog;
	
	public DrinksBookCartListAdapter(DrinksBookCartWidget drinksBookCartWidget, Context context,List<Drinks> list) {
		mContext = context;
		mList = list;
		this.drinksBookCartWidget = drinksBookCartWidget;
		mThis = this;
		mDialog = new AlertDialog.Builder(mContext);
		asyncBitmapLoader = AsyncBitmapLoader.getInstance();
	}
	
	public int getCount() {
		if(mList == null) {
			return 0;
		}
		return mList.size();
	}
	
	public Object getItem(int position) {
		if(mList == null) {
			return null;
		} else {
			return mList.get(position);
		}
	}
	
	public long getItemId(int position) {
		return position;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {

		View view;
		
		if(convertView == null) {
			vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = (LinearLayout) vi.inflate(R.layout.drinks_book_cart_list_item, null);
		} else {
			view = convertView;
		}
		FillDataIntoView(view, position);
		
		return view;
	}
	
	private void FillDataIntoView(View view, final int position) {
		final Drinks drinks = mList.get(position);

		ImageView img = (ImageView)view.findViewById(R.id.drinks_book_drink_img);
		TextView tv_name = (TextView)view.findViewById(R.id.drinks_book_drink_name_textview);
		TextView tv_price = (TextView)view.findViewById(R.id.drinks_book_drink_price_textview);
		//TextView tv_priceMarket = (TextView)view.findViewById(R.id.drinks_book_drink_price_market_textview);
		TextView tv_num = (TextView)view.findViewById(R.id.drinks_book_drink_num_textview);
		TextView tv_priceTotal = (TextView)view.findViewById(R.id.drinks_book_drink_price_total_textview);
		Button bt_cancel = (Button)view.findViewById(R.id.drink_book_cancel_button);

		float price = drinks.getPriceTip();
		int num = drinks.getNum();
		TextPaint tp = tv_name.getPaint(); 
		tp.setFakeBoldText(true); 
		tv_name.setText(drinks.getName());
		tv_price.setText(price + "");
		//tv_priceMarket.setText(drinks.getPriceMarket() +"");
		tv_num.setText(num +"");
		tv_priceTotal.setText(price*num +"");
		bt_cancel.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showRemoveDialog(drinks.getName(), position);
			}
		});
		
		String imageURL = drinks.getImgURL();  
        Bitmap bitmap = asyncBitmapLoader.loadBitmap(img, imageURL, new ImageCallBack() {  
              
            public void imageLoad(ImageView imageView, Bitmap bitmap) {  
                // TODO Auto-generated method stub  
                imageView.setImageBitmap(bitmap);  
            }  
        });  
		if(bitmap == null) {    
			img.setImageResource(R.drawable.ic_launcher);    
		} else {    
		    img.setImageBitmap(bitmap);    
		}
	    
	}

	@Override
	public boolean isEnabled(int position) {
		// TODO Auto-generated method stub
		return super.isEnabled(position);
	}
	protected void showRemoveDialog(String message, final int position){
        mDialog.setTitle("购物车")
        .setMessage("从购物车中移除 \"" + message + "\" ?");
        mDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				mList.remove(position);
				mThis.notifyDataSetChanged();
				drinksBookCartWidget.changePriceTotal();
			}
		});
        mDialog.setNegativeButton("取消", null);
        
        mDialog.setCancelable(true);
        mDialog.show();
    }
}
